package com.hzz.framework;

import com.hzz.dto.ZTreeNodeDto;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.ReflectUtil;
import com.hzz.framework.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MultiTree {
    String pkAttrId;
    String[] attrIds;
    Map<String, ZTreeNodeDto> attrMap;
    List dataList;
    List<ZTreeNodeDto> treeList;

    public MultiTree(String pkAttrId, List dataList, String... attrIds) {
        this.pkAttrId = pkAttrId;
        this.dataList = dataList;
        this.attrIds = attrIds;
        this.attrMap = new HashMap<>();
        this.treeList = new ArrayList<>();
        if (attrIds == null || attrIds.length < 2) {
            throw new RuntimeException("TreeNode必须至少一组父子结构字段!");
        }
    }

    public List<ZTreeNodeDto> buildTree() {
        for (Object data : dataList) {
            String parentKey = "";
            for (int i = 0; i < attrIds.length - 1; i++) {
                boolean xxFlag = false;
                String parentAttrId = attrIds[i];
                String pkId = null;
                if (pkAttrId != null) {
                    pkId = "" + ReflectUtil.getValueByFieldName(data, pkAttrId);
                }
                String parentValue = "" + ReflectUtil.getValueByFieldName(data, parentAttrId);
                parentKey += CommonConstant.JWT_HEADER_TOKEN_PREFIX + parentAttrId + "=" + parentValue;
                ZTreeNodeDto parentZTreeNodeDto = attrMap.get(parentKey);
                if (parentZTreeNodeDto == null) {
                    parentZTreeNodeDto = new ZTreeNodeDto();
                    parentZTreeNodeDto.setId(pkId);
                    parentZTreeNodeDto.setData(i);
                    parentZTreeNodeDto.setName(parentValue);
                    parentZTreeNodeDto.setChildren(new ArrayList<>());
                    attrMap.put(parentKey, parentZTreeNodeDto);
                    if (i == 0) {
                        treeList.add(parentZTreeNodeDto);
                    }
                }
                String childAttrId = attrIds[i + 1];
                String childValue = "" + ReflectUtil.getValueByFieldName(data, childAttrId);
                if (ValidatorUtil.notEmpty(childValue)) {
                    String childKey = parentKey + CommonConstant.JWT_HEADER_TOKEN_PREFIX + childAttrId + "=" + childValue;
                    ZTreeNodeDto childZTreeNodeDto = attrMap.get(childKey);
                    if (childZTreeNodeDto == null) {
                        childZTreeNodeDto = new ZTreeNodeDto();
                        childZTreeNodeDto.setId(pkId);
                        childZTreeNodeDto.setName(childValue);
                        childZTreeNodeDto.setChildren(new ArrayList<>());
                        attrMap.put(childKey, childZTreeNodeDto);
                        parentZTreeNodeDto.setParent(true);
                        parentZTreeNodeDto.getChildren().add(childZTreeNodeDto);
                        if (i + 1 == attrIds.length - 1 || xxFlag) {
                            childZTreeNodeDto.setData(data);
                        } else {
                            childZTreeNodeDto.setData(i + 1);
//                            childZTreeNodeDto.setParent(true);
                        }
                    }
                }
            }
        }
        return treeList;
    }
}